package io.xxx.xbutler.core.get;

import com.alibaba.fastjson2.JSONObject;
import io.xxx.xbutler.client.PromotionClient;
import io.xxx.xbutler.domain.Poster;
import io.xxx.xbutler.domain.task.ContentType;
import io.xxx.xbutler.domain.task.MessageType;
import io.xxx.xbutler.domain.task.PushContext;
import io.xxx.xbutler.domain.task.TaskItem;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class PosterGetter implements ContentGetter {

    @Resource
    private PromotionClient promotionClient;

    @Override
    public void replaceContent(PushContext context) {
        List<TaskItem> taskItems = context.getTaskItems();
        for (TaskItem taskItem : taskItems) {
            if (taskItem.getMessageType() != MessageType.IMAGE || taskItem.getContentType() != ContentType.POSTER) {
                continue;
            }

            JSONObject content = taskItem.getContent();
            String skuSn = content.getString("skuSn");
            Poster poster = promotionClient.getPoster(skuSn);
            content.put("url", poster.getBackgroundImage());
            taskItem.setContent(content);
        }
    }
}
